package com.example.cursor_animation

import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.editor.event.CaretEvent
import com.intellij.openapi.editor.event.CaretListener
import com.intellij.openapi.editor.event.EditorFactoryEvent
import com.intellij.openapi.editor.event.EditorFactoryListener
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import java.awt.Cursor
import java.awt.event.ActionEvent
import javax.swing.JComponent
import javax.swing.Timer
var num = 0 ;
class cursorSmoothCaretAnimation   :ProjectActivity{

    private fun applyCursorTransition(editorComponent: JComponent) {
        //println ( "cursorSmoothCaretAnimation    start  apply  $num ")
         num =num +1;
        editorComponent.putClientProperty("JComponent.outline", "none")
        editorComponent.cursor = Cursor(Cursor.TEXT_CURSOR)
        // Apply CSS transition using a Timer for simplicity
        val timer = Timer(80) { e: ActionEvent? ->
            editorComponent.repaint()
            //println ("editor repaint  pass by  80ms ")
        }
        timer.isRepeats = false
        timer.start()
    }

    override suspend fun execute(project: Project) {
            EditorFactory.getInstance().addEditorFactoryListener(object : EditorFactoryListener {
                override fun editorCreated(event: EditorFactoryEvent) {
                    event.editor.caretModel.addCaretListener(object : CaretListener {
                        override fun caretPositionChanged(e: CaretEvent) {
                            applyCursorTransition(e.editor.contentComponent)
                        }
                    })
                }
            }) {}
        }

    }
